package io.renren.modules.store.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.common.utils.BeanJudgeUtil;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStoreOrderSourceEntity;
import io.renren.modules.store.service.TStoreOrderSourceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-25 02:05:20
 */
@RestController
@RequestMapping("store/tstoreordersource")
public class TStoreOrderSourceController extends AbstractController {
    @Autowired
    private TStoreOrderSourceService tStoreOrderSourceService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstoreordersource:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        PageUtils page = tStoreOrderSourceService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{orderSourceId}")
    @RequiresPermissions("store:tstoreordersource:info")
    public R info(@PathVariable("orderSourceId") Long orderSourceId){
		TStoreOrderSourceEntity tStoreOrderSource = tStoreOrderSourceService.getById(orderSourceId);

        return R.ok().put("tStoreOrderSource", tStoreOrderSource);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit(lockTime = 5)
    @RequestMapping("/save")
    @RequiresPermissions("store:tstoreordersource:save")
    public R save(@RequestBody TStoreOrderSourceEntity tStoreOrderSource){
        TStoreOrderSourceEntity one = tStoreOrderSourceService.getOne(new QueryWrapper<TStoreOrderSourceEntity>()
                .eq("seller_id", getSeller())
                .eq("order_source_name", tStoreOrderSource.getOrderSourceName())
        );
        if (!BeanJudgeUtil.beanIsNull(one)){
            throw new RRException("消费来源已存在");
        }
        TStoreOrderSourceEntity one1 = tStoreOrderSourceService.getOne(new QueryWrapper<TStoreOrderSourceEntity>()
                .eq("system_built_in", 1)
                .eq("order_source_name", tStoreOrderSource.getOrderSourceName())
        );
        if (!BeanJudgeUtil.beanIsNull(one1)){
            throw new RRException("此消费来源系统已内置，不能重复添加");
        }
        tStoreOrderSource.setSellerId(getSeller());
		tStoreOrderSourceService.save(tStoreOrderSource);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstoreordersource:update")
    public R update(@RequestBody TStoreOrderSourceEntity tStoreOrderSource){
		tStoreOrderSourceService.updateById(tStoreOrderSource);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstoreordersource:delete")
    public R delete(@RequestBody Long[] orderSourceIds){
		tStoreOrderSourceService.removeByIds(Arrays.asList(orderSourceIds));

        return R.ok();
    }

}
